package com.nlx.notes.module.core.util;

import cn.hutool.core.util.StrUtil;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class DateUtils {

    private static final String PATTERN_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd hh:mm:ss";

    private static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";

    private static final String PATTERN_YYYY_MM = "yyyy-MM";

    private static final String PATTERN_YYYY= "yyyy";

    private static final String PATTERN_DD_MM= "MM-dd";


    public static LocalDateTime parseDateTime(String time){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return  LocalDateTime.parse(time, formatter);
    }

    public static LocalDate parseDate(String time){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return  LocalDate.parse(time.substring(0,10), formatter);
    }

    public static YearMonth parseMonthDate(String time){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_YYYY_MM);
        return  YearMonth.parse(time, formatter);
    }

    public static LocalDate parseMonthDate(YearMonth yearMonth){
        return  yearMonth.atDay(1);
    }

    public static LocalDate parseYearDate(String time){
        Year year = Year.parse(time, DateTimeFormatter.ofPattern(PATTERN_YYYY));
        return  year.atDay(1);  // 2025-01-01
    }



    /**
     * 获取当前日期
     * @return String
     */
    public static String formatCurrentDate() {
        LocalDate date = getCurrentDate();
        return formatDate(date);
    }

    public static String formatDate(LocalDate date) {
        return date.format(DateTimeFormatter.ofPattern(PATTERN_YYYY_MM_DD));
    }

    public static String formatDate(LocalDateTime date) {
        return date.format(DateTimeFormatter.ofPattern(PATTERN_YYYY_MM_DD));
    }


    public static String formatCurrentDateTime() {
        LocalDateTime date = getCurrentDateTime();
        return formatDateTime(date);
    }

    public static String formatDateTime(LocalDateTime date) {
        return date.format(DateTimeFormatter.ofPattern(PATTERN_YYYY_MM_DD_HH_MM_SS));
    }

    public static LocalDateTime getCurrentDateTime() {
        return LocalDateTime.now();
    }

    public static LocalDate getCurrentDate() {
        return LocalDate.now();
    }

    public static String formatCurrentMonth() {
        LocalDate date = getCurrentDate();
        return date.format(DateTimeFormatter.ofPattern(PATTERN_YYYY_MM));
    }

    public static String formatMonth(LocalDateTime date) {
        return date.format(DateTimeFormatter.ofPattern(PATTERN_YYYY_MM));
    }

    public static String formatMonth(LocalDate date) {
        return date.format(DateTimeFormatter.ofPattern(PATTERN_YYYY_MM));
    }

    public static String formatMonth(YearMonth date) {
        return date.format(DateTimeFormatter.ofPattern(PATTERN_YYYY_MM));
    }



    public static String formatCurrentYear() {
        return formatCurrentYear(LocalDate.now());
    }

    public static String formatCurrentYear(LocalDate date) {
        return date.format(DateTimeFormatter.ofPattern(PATTERN_YYYY));
    }

    public static String formatCurrentYear(LocalDateTime date) {
        return date.format(DateTimeFormatter.ofPattern(PATTERN_YYYY));
    }



    public static String formatCurrentWeek() {
        LocalDate currentDate = getCurrentDate();
        LocalDate startOfWeek = currentDate.with(DayOfWeek.MONDAY);
        // 获取本周的周日（结束日期）
        LocalDate endOfWeek = currentDate.with(DayOfWeek.SUNDAY);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_DD_MM);
        // 转换为字符串
        String startOfWeekStr = startOfWeek.format(formatter);
        String endOfWeekStr = endOfWeek.format(formatter);
        return startOfWeekStr + " ~ " + endOfWeekStr;
    }


    public static String formatCurrentStartWeek(LocalDate nowDate) {
        LocalDate startOfWeek = nowDate.with(DayOfWeek.MONDAY);
        return startOfWeek.toString();

    }

    public static String formatCurrentEndWeek(LocalDate nowDate) {
        // 获取本周的周日（结束日期）
        LocalDate endOfWeek = nowDate.with(DayOfWeek.SUNDAY);
        // 转换为字符串
        return endOfWeek.toString();
    }


    public static String formatCurrentStartMonth(LocalDate nowDate) {
        LocalDate firstDayOfMonth = nowDate.withDayOfMonth(1);
        return firstDayOfMonth.format(DateTimeFormatter.ofPattern(PATTERN_YYYY_MM_DD));
    }

    public static String formatCurrentEndMonth(LocalDate nowDate) {
        LocalDate firstDayOfMonth = nowDate.withDayOfMonth(nowDate.lengthOfMonth());
        return firstDayOfMonth.format(DateTimeFormatter.ofPattern(PATTERN_YYYY_MM_DD));
    }

    /**
     * 获取本年的第一天
     * @param nowDate 当前日期
     * @return String
     */
    public static String formatCurrentStartYear(LocalDate nowDate) {
        LocalDate firstDayOfYear = nowDate.with(TemporalAdjusters.firstDayOfYear());
        return firstDayOfYear.format(DateTimeFormatter.ofPattern(PATTERN_YYYY_MM_DD));
    }

    /**
     * 获取本年的最后一天
     * @param nowDate 当前日期
     * @return String
     */
    public static String formatCurrentEndYear(LocalDate nowDate) {
        LocalDate lastDayOfYear = nowDate.with(TemporalAdjusters.lastDayOfYear());
        return lastDayOfYear.format(DateTimeFormatter.ofPattern(PATTERN_YYYY_MM_DD));
    }

    /**
     * 获取本月份的第一天
     * @return String
     */
    public static String formatFirstDayForCurrentMonth() {
        return formatFirstDayFortMonth(LocalDate.now());
    }

    public static String formatFirstDayFortMonth(LocalDate date) {
        LocalDate firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
        return formatDate(firstDay);
    }

    public static String formatFirstDayFortMonth(YearMonth date) {
        LocalDate firstDay = date.atDay(1);
        return formatDate(firstDay);
    }


    /**
     * 获取本月份的最后一天
     * @return String
     */
    public static String formatLastDayForCurrentMonth() {
        return formatLastDayForMonth(LocalDate.now());

    }

    public static String formatLastDayForMonth(LocalDate date) {
        LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
        return formatDate(lastDay);
    }

    public static String formatLastDayForMonth(YearMonth date) {
        LocalDate lastDay = date.atEndOfMonth();
        return formatDate(lastDay);
    }



    public static String formatFirstDayForCurrentYear() {
        return formatFirstDayFortYear(LocalDate.now());
    }

    public static String formatFirstDayFortYear(LocalDate date) {
        LocalDate firstDay = date.with(TemporalAdjusters.firstDayOfYear());
        return formatDate(firstDay);
    }

    public static String formatLastDayForCurrentYear() {
        return formatLastDayForMonth(LocalDate.now());

    }

    public static String formatLastDayForYear(LocalDate date) {
        LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfYear());
        return formatDate(lastDay);
    }


    public static String yearMonthToMonth(String yearMonth){
        if(StrUtil.isEmpty(yearMonth)){
            return null;
        }
        return yearMonth.substring(5);
    }




}
